<link rel="stylesheet" href="${ctx}/static/admin/assets/css/jquery-ui.custom.css" />
<link rel="stylesheet" href="${ctx}/static/admin/assets/css/chosen.css" />
<link rel="stylesheet" href="${ctx}/static/admin/assets/css/datepicker.css" />
<form id="adItemForm" class="form-horizontal" action="${ctx}/admin/ad/saveAdItem.do"  method="post">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h5 class="blue">编辑广告内容</h5>
    </div>
    <input id="imageUrl" name="imageUrl" type="hidden"/>
    <input type="hidden" name="id" id="id" class="col-xs-12 col-sm-4 required" value="${adItem.id!}"/>
    <div class="modal-body">
        <!--广告标题-->
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="adItemTitle">广告标题:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <input type="text" name="adItemTitle" id="adItemTitle" class="col-xs-12 col-sm-4 required" value="${adItem.adItemTitle!}"/>
                </div>
            </div>
        </div>
        <!--所属广告位-->
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="adSpaceId">所属广告位:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <select class="input-medium" id="adSpaceId" name="adSpaceId">
                        <#list adSpaceList as adSpace>
                            <option value="${adSpace.id!}" <#if adItem.adSpaceId?? && adSpace.id = adItem.adSpaceId>selected="selected"</#if> >${adSpace.adSpaceName!}</option>
                        </#list>
                    </select>
                </div>
            </div>
        </div>
        <!--状态-->
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right">状态:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <select class="input-medium" id="state" name="state">
                        <option value="1" <#if  !adItem.state?? || (adItem.state?? && adItem.state = 1)>selected="selected"</#if>>显示</option>
                        <option value="0" <#if  adItem.state?? && adItem.state = 0>selected="selected"</#if>>隐藏</option>
                        <option value="-1" <#if  adItem.state?? && adItem.state = -1>selected="selected"</#if>>删除</option>
                    </select>
                </div>
            </div>
        </div>
        <!--类型-->
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right">资源类型:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <select class="input-medium" id="resourceType" name="resourceType">
                        <option value="1" <#if  !adItem.resourceType?? || (adItem.resourceType?? && adItem.resourceType = 1) >selected="selected"</#if>>软件详情</option>
                        <option value="2" <#if  adItem.resourceType?? && adItem.resourceType = 2>selected="selected"</#if>>软件分类</option>
                        <option value="3" <#if  adItem.resourceType?? && adItem.resourceType = 3>selected="selected"</#if>>专题</option>
                        <option value="4" <#if  adItem.resourceType?? && adItem.resourceType = 4>selected="selected"</#if>>外链</option>
                    </select>
                </div>
            </div>
        </div>
        <!--外链-->
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="linkUrl">外链:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <input type="text" name="linkUrl" id="linkUrl" class="col-xs-12 col-sm-4" value="${adItem.linkUrl!}"/>
                </div>
            </div>
        </div>


        <!--资源-->
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="resourceValue">资源值:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <input type="number" name="resourceValue" id="resourceValue" class="col-xs-12 col-sm-4 required " max="99999999999"  value="${adItem.resourceValue!}"/>
                </div>
            </div>
        </div>

        <!--开始时间-->
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="startDate">开始时间:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="input-medium input-group">
                    <span style="position: relative; z-index: 9999;">
                    <input type="text" name="startDate" id="startDate" class="input-medium date-picker" value="${adItem.startTimeShow!}" readonly  data-date-format="yyyy-mm-dd"/>
                    </span>
                    <span class="input-group-addon"> <i class="ace-icon fa fa-calendar"></i></span>
                </div>
            </div>
        </div>
        <!--结束时间-->
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="expiredDate">结束时间:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="input-medium input-group">
                    <span style="position: relative; z-index: 9999;">
                    <input type="text" name="expiredDate" id="expiredDate" class="input-medium date-picker" value="${adItem.expiredTimeShow!}" readonly  data-date-format="yyyy-mm-dd"/>
                    </span>
                    <span class="input-group-addon"> <i class="ace-icon fa fa-calendar"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="sortIndex">显示顺序:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <span class="input-icon input-icon-right">
                        <input type="sortIndex" name="sortIndex" id="sortIndex" value="${adItem.sortIndex!}" />
                    </span>
                </div>
            </div>-
        </div>
        <!----上传广告图-------->
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="sortIndex">上传广告图:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="widget-body">
                    <div class="widget-main">
                        <div class="form-group">
                            <div class="col-xs-8">
                                <!-- #section:custom/file-input -->
                                <input type="file" id="id-input-ad" name="adFile"/>
                            </div>
                            <button type="button" id="id-button-ad" class="btn btn-sm btn-success" onclick="uploadAdFile()">
                                上传 <i class="ace-icon fa fa-arrow-right icon-on-right bigger-60"></i>
                            </button>

                        </div>
                    </div>
                </div>

                <div class="widget-body">
                    <div class="widget-main">
                        <div>
                            <ul class="ace-thumbnails clearfix">

                                <li><a id="ada" name="ada" data-rel="colorbox">
                                    <img id="ad" name="ad" width="150" src="${staticHost}${adItem.imageUrl!}"  onerror="this.src='${ctx}/static/admin/assets/img/imgerror.png'"/>
                                </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>



    <div class="modal-footer center">
        <button type="button" onclick="saveAd()" class="btn btn-sm btn-success"><i class="ace-icon fa fa-check"></i> 保存</button>
        <button type="button" class="btn btn-sm" data-dismiss="modal"><i class="ace-icon fa fa-times"></i> 取消</button>
    </div>
</form>
<script src="${ctx}/static/admin/assets/js/jquery.validate.js"></script>
<script src="${ctx}/static/admin/assets/js/jquery.validate.messages_zh.js"></script>
<script src="${ctx}/static/admin/assets/js/ajaxfileupload.js"></script>
<script src="${ctx}/static/admin/assets/js/bootstrap-wysiwyg.js"></script>
<script src="${ctx}/static/admin/assets/js/jquery.validate.js"></script>
<script src="${ctx}/static/admin/assets/js/jquery.validate.messages_zh.js"></script>
<script type="text/javascript">

    function uploadAdFile(){
        $.blockUI();
        $.ajaxFileUpload({
            url:"${ctx}/admin/upload/uploadAdFile.do?inputName=adFile",            //需要链接到服务器地址
            dataType : 'text',
            fileElementId:'id-input-ad',                        //文件选择框的id属性
            success: function(data, status){
                $.unblockUI();
                msg = jQuery.parseJSON(jQuery(data).text());
                if(msg.result == "success"){
                    var upload=msg.data;
                    $("#ad").attr("src","${staticHost}"+upload.adUrl);
                    $("#ada").attr("href","${staticHost}"+upload.adUrl);
                    $("#imageUrl").val(upload.adUrl);
                }else{
                    alert(msg.messages);
                }
            },error: function (data, status, e){
                $.unblockUI();
                alert("上传出错，请重新上传");
            }
        });
    }

    $('#id-input-apk,#id-input-icon,#id-input-gamecut,#id-input-ad,#id-input-screenshot').ace_file_input({
        no_file:'未选择文件 ...',
        btn_choose:'浏览',
        btn_change:'更改',
        droppable:false,
        onchange:null,
        thumbnail:false //| true | large
        //whitelist:'gif|png|jpg|jpeg'
        //blacklist:'exe|php'
        //onchange:''
        //
    });
  
    function saveAd(){
        if($("#resourceType").val()==4){
            if($("#linkUrl").val()==""){
                alert("请填写外链!");
                return false;
            }
        }else{
            if($("#resourceValue").val()==""){
                alert("请填写资源值!");
                return false;
            }
        }
        if ($("#imageUrl").val() == null){
            alert("请上传广告图");
            return false;
        }
        $("#adItemForm").submit();
    }

    $(document).ready(function(){
        $(".date-picker").datepicker({
            language : 'zh-CN',
            autoclose: true,
            todayHighlight: true
        });

        $("#adItemForm").validate({
            errorElement: 'div',
            errorClass: 'help-block',
            focusInvalid: true,
            highlight: function (e) {
                $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
            },
            success: function (e) {
                $(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
                $(e).remove();
            },
            errorPlacement: function (error, element) {
                if(element.is('input[type=checkbox]') || element.is('input[type=radio]')) {
                    var controls = element.closest('div[class*="col-"]');
                    if(controls.find(':checkbox,:radio').length > 1) controls.append(error);
                    else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
                }
                else if(element.is('.select2')) {
                    error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
                }
                else if(element.is('.chosen-select')) {
                    error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
                }
                else error.insertAfter(element.parent());
            },

            submitHandler: function (form) {
                    if(timeCompareFailed($("#startDate").val(),$("#expiredDate").val())){
                        alert("过期时间需要大于开始时间");
                        return false;
                    }
                    $.ajax({
                        cache: true,
                        type: "POST",
                        url:"${ctx}/admin/ad/saveAdItem.do",
                        data:$('#adItemForm').serialize(),// 你的formid
                        async: false,
                        error: function(request) {
                            alert("Connection error");
                        },
                        success: function(data) {
                            if(data.result == "success"){
                                window.location="${refer!}";
                            }else{
                                alert(data.messages);
                            }
                        }
                    });

            },
            invalidHandler: function (form) {
            }
        })
    });
</script>